//
//  NSMMacros.h
//  NSMKit
//
//  Created by nsm on 2019/2/26.
//  Copyright © 2019 nani. All rights reserved.
//

#ifndef NSMMacros_h
#define NSMMacros_h

#ifndef _noteCenter
#define _noteCenter             ([NSNotificationCenter defaultCenter])
#endif

#define FormatString(fmt, ...)  [NSString stringWithFormat:fmt, ##__VA_ARGS__]

#define SetBit(n, i)            ((n)|=(0x01<<(i)))
#define GetBit(n, i)            (((n)&(0x01<<(i)))?1:0)
#define ClearBit(n, i)          ((n)&=(~(0x01<<(i))))

#define nsm_weak(obj)           autoreleasepool{} __weak typeof(obj) weak##obj = obj;
#define nsm_strong(obj)         autoreleasepool{} __strong typeof(weak##obj) obj = weak##obj;

#ifndef NSM_DYNAMIC_PROPERTY_OBJECT
#define NSM_DYNAMIC_PROPERTY_OBJECT(_type_, _getter_, _setter_, _policy_) \
- (_type_)_getter_ { \
    return objc_getAssociatedObject(self, _cmd); \
} \
- (void)_setter_:(_type_)obj { \
    [self willChangeValueForKey:@#_getter_]; \
    objc_setAssociatedObject(self, @selector(_getter_), obj, OBJC_ASSOCIATION_##_policy_); \
    [self didChangeValueForKey:@#_getter_]; \
}

#define NSM_DYNAMIC_PROPERTY_ASSIGN(_type_, _value_type_, _getter_, _setter_) \
- (_type_)_getter_ { \
    NSNumber *temp = objc_getAssociatedObject(self, _cmd); \
    return temp._value_type_; \
} \
- (void)_setter_:(_type_)obj { \
    [self willChangeValueForKey:@#_getter_]; \
    objc_setAssociatedObject(self, @selector(_getter_), @(obj), OBJC_ASSOCIATION_ASSIGN); \
    [self didChangeValueForKey:@#_getter_]; \
}
#endif

#endif /* NSMMacros_h */
